package util

import (
	"github.com/kordar/k-means-server/src/pojo"
	"sort"
)

func ArrayStructGroupsOf(stu []pojo.FeatureItem, num int64, repeat int) [][]pojo.FeatureItem {
	max := int64(len(stu))
	// 判断数组大小是否小于等于指定分割大小的值，是则把原数组放入二维数组返回
	if max <= num {
		return [][]pojo.FeatureItem{stu}
	}
	// 获取应该数组分割为多少份
	var quantity int64
	if max%num == 0 {
		quantity = max / num
	} else {
		quantity = (max / num) + 1
	}
	// 声明分割好的二维数组
	var segments = make([][]pojo.FeatureItem, 0)
	// 声明分割数组的截止下标
	var start, end, i int64
	for i = 1; i <= quantity; i++ {
		end = i * num
		if i != quantity {
			segments = append(segments, stu[start:end])
		} else {
			segments = append(segments, stu[start:])
		}
		start = i*num - int64(repeat)
	}
	return segments
}

type ValSorter struct {
	Keys []int
	Vals []float64
}

func NewValSorter(m map[int]float64) *ValSorter {
	vs := &ValSorter{
		Keys: make([]int, 0, len(m)),
		Vals: make([]float64, 0, len(m)),
	}
	for k, v := range m {
		vs.Keys = append(vs.Keys, k)
		vs.Vals = append(vs.Vals, v)
	}
	return vs
}

func (vs *ValSorter) Sort() {
	sort.Sort(vs)
}

func (vs *ValSorter) Len() int           { return len(vs.Vals) }
func (vs *ValSorter) Less(i, j int) bool { return vs.Vals[i] < vs.Vals[j] }
func (vs *ValSorter) Swap(i, j int) {
	vs.Vals[i], vs.Vals[j] = vs.Vals[j], vs.Vals[i]
	vs.Keys[i], vs.Keys[j] = vs.Keys[j], vs.Keys[i]
}
